<?php
/*
Cogumelo v0.1 - Innoto
Copyright (C) 2009 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@innoto.es>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
*/

require_once("Classes/Model/Facade/ResourceFacade.inc");
require_once("Classes/Model/Facade/ResourceclassFacade.inc");
require_once("Classes/Model/Facade/ActivityFacade.inc");
require_once("Classes/Model/VO/ResourceVO.inc");
require_once("Classes/Model/VO/ResourceclassVO.inc");
require_once("Classes/Model/VO/ActivityVO.inc");
require_once("Classes/Controllers/Data/DataController.inc");

//
//
// Resource Controller Class
//
//make
class  ResourceController extends DataController
{
	private $data;
	
	//
	//	class constructor, creates ResourceFacade object
	//
	function __construct()
	{	
		parent::__construct();
		$this->data = new ResourceFacade();
	}
	

	////////////////////////////////////////////
	////////// Find And List Methods //////////
	//////////////////////////////////////////
	
	//
	//	Find by ID
	//
	function Find($id) 
	{
		$data = $this->data->Find($id);
		
		if($data) $this->logcontrol->addLogLine("Resource with ID=".$id." FOUND", 3);
		else $this->logcontrol->addLogLine("Resource with ID=".$id." NOT FOUND", 3);
		
		return $data;
	}
	
	
	////////////////////////////////////////////
	/////////////  Input Methods //////////////
	//////////////////////////////////////////
	
	
	//
	//	Create new Resource (from a hash of data)
	//
	function Create($resource, $activities, $classes)		
	{
		if($activities)
		foreach($activities as $activity)
		{
			$activityVO= new ActivityVO($activity['id'],0,0,0,0,0,0,0,$activity['category'],$activity['adapted'],$activity['age1'],$activity['age2'],$activity['age3'],$activity['age4']);
			$activityVOs[]=$activityVO;			
		}
		
		if($classes)
		foreach($classes as $class)
		{
			$classVO= new ResourceclassVO($class['id'],0,0,0,0,0,0,0,0,$class['ceil']);
			$classVOs[]=$classVO;			
		}
		
		$rightnow=date("YmdHis", time());	
		  		
		$newresource = new ResourceVO(
							null,
							$resource['resource_status'],
							$rightnow,
							$rightnow,
							$resource['resource_user'],
							$resource['resource_defaulticon'],
							$resource['resource_location'],
							$resource['resource_concello'],
							$resource['resource_prov'],
							$resource['resource_name_es'],
							$resource['resource_name_gl'],
							$resource['resource_name_en'],
							$resource['resource_text_es'],
							$resource['resource_text_gl'],
							$resource['resource_text_en'],
							$resource['resource_opening_es'],
							$resource['resource_opening_gl'],
							$resource['resource_opening_en'],
							$resource['resource_email'],
							$resource['resource_tel'],
							$resource['resource_web'],
							$resource['resource_address'],
							$resource['resource_accesible'],
							$resource['resource_cp'],
							$activityVOs,
							$classVOs
						);	
						
		$data = $this->data->Create($newresource);
		
		if($data) $this->logcontrol->addLogLine("Resource created with ID=".$data, 3);
		else $this->logcontrol->addLogLine("Unable to create Resource", 3);
		
		return $data;
		
	}
	
	
	
    //
	//	Update resource data from a hash of data 
	function Update($resource, $activities, $classes)		
	{	
		if($activities)
		foreach($activities as $activity)
		{
			$activityVO= new ActivityVO($activity['id'],0,0,0,0,0,0,0,$activity['category'],$activity['adapted'],$activity['age1'],$activity['age2'],$activity['age3'],$activity['age4']);
			$activityVOs[]=$activityVO;			
		}
		
		if($classes)
		foreach($classes as $class)
		{
			$classVO= new ResourceclassVO($class['id'],0,0,0,0,0,0,0,0,$class['ceil']);
			$classVOs[]=$classVO;			
		}
		
		$rightnow=date("YmdHis", time());
				
		$newresource = new ResourceVO(
							$resource['resource_id'],
							$resource['resource_status'],
							0,
							$rightnow,
							$resource['resource_user'],
							$resource['resource_defaulticon'],
							$resource['resource_location'],
							$resource['resource_concello'],
							$resource['resource_prov'],
							$resource['resource_name_es'],
							$resource['resource_name_gl'],
							$resource['resource_name_en'],
							$resource['resource_text_es'],
							$resource['resource_text_gl'],
							$resource['resource_text_en'],
							$resource['resource_opening_es'],
							$resource['resource_opening_gl'],
							$resource['resource_opening_en'],
							$resource['resource_email'],
							$resource['resource_tel'],
							$resource['resource_web'],
							$resource['resource_address'],
							$resource['resource_accesible'],
							$resource['resource_cp'],
							$activityVOs,
							$classVOs
							);					
							
		$data = $this->data->Update($newresource);
		if($data) $this->logcontrol->addLogLine("resource updated with ID=".$data, 3);
		else $this->logcontrol->addLogLine("Unable to update resource", 3);
		
		return $data;
		
	}

	
	//
	//	Update Resource status.
	//
	function UpdateStatus($id, $status)									
	{
	  $data = $this->data->UpdateStatus($id, $status);
	  
	  	if($data) $this->logcontrol->addLogLine("Status changed for Resource with ID=".$id, 3);
		else $this->logcontrol->addLogLine("Status to change Passwd for Resource with ID=".$id, 3);
		
		return $data;	  	
	}
	
	
	//
	//	Delete resource
	//
	function Delete($id)
	{
		$data = $this->data->Delete($id);
		if($data) $this->logcontrol->addLogLine("Resource with ID=".$id." removed but not destroyed", 3);
		else $this->logcontrol->addLogLine("Unable to delete Resource with ID=".$id, 3);
		
		return $data;
	}
	
	//
	//	Destroy resource
	//
	function Destroy($id)
	{
		$data = $this->data->Destroy($id);
		if($data) $this->logcontrol->addLogLine("Resource with ID=".$id." destroyed", 3);
		else $this->logcontrol->addLogLine("Unable to remove Resource with ID=".$id, 3);
		
		return $data;
		
	}
	
	//
	//	Publish a resource
	//
	function Publish($id)
	{
		$data = $this->data->Publish($id);
		if($data) $this->logcontrol->addLogLine("Resource with ID=".$id." published", 3);
		else $this->logcontrol->addLogLine("Unable to preselect Resource with ID=".$id, 3);
		
		return $data;
	}	
	
	//
	//	Hide a resource
	//
	function Hide($id)
	{
		$data = $this->data->Hide($id);
		if($data) $this->logcontrol->addLogLine("Resource with ID=".$id." hidden", 3);
		else $this->logcontrol->addLogLine("Unable to hide Resource with ID=".$id, 3);

		return $data;
	}	
	
	
		
	//
	// List  Resources (Light version)
	//
	function ListResources($RANGE, $ORDER, $filter = null)
	{
		$data = $this->data->ListResources($filter, $RANGE, $ORDER);
		if($data) $this->logcontrol->addLogLine("Resources list Generated", 3);
		else $this->logcontrol->addLogLine("EMPTY Resources list Generated", 3);
		
		return $data;
	}
	//
	// List  Resources (Complete version)
	//
	function ListResourcesComplete($RANGE, $ORDER, $filter = null)
	{
		$data = $this->data->ListResourcesComplete($filter, $RANGE, $ORDER);
		if($data) $this->logcontrol->addLogLine("Resources list Generated", 3);
		else $this->logcontrol->addLogLine("EMPTY Resources list Generated", 3);	
		
		return $data;
	}
	
	//
	// Count Resources (Rows number)
	function ListResourcesCount($filter)
	{
		$data = $this->data->ListResourcesCount($filter);
		$this->logcontrol->addLogLine("Counted ".$data." Resources", 3);
				
		return $data;
	}
	
	
	
	
	

}
